/**
 * @author xuyh0817
 * @email xuyh0817@foxmail.com
 * @date 2022-03-12 21:59
 * @version 2.0.0
 * @doc todo
 * @website todo
 * @remark  如果要分发源码，需在本文件顶部保留此文件头信息！！
 */
import { upperFirst } from 'lodash'
import { SLOT_PREFIX } from '../constants'

export default {
  name: 'ExtendSlotMixin',
  inject: ['xRenderForm'],
  methods: {
    getSlotMap() {
      const { xRenderForm } = this
      const slotMap = xRenderForm.$scopedSlots
      if (!slotMap) {
        throw new Error('slotMap未找到')
      }
      return slotMap
    },
    /**
     * 是否有slot
     * @param name {string} 插槽名称
     */
    hasSlot(name) {
      if (!name) {
        return false
      }
      const slotMap = this.getSlotMap()
      const keys = Object.keys(slotMap)
      return keys.includes(SLOT_PREFIX + this.upperNameFirstChar(name))
    },
    /**
     * 插槽首字母大写
     *
     * @param name {string} 插槽名称
     * @returns {string}
     */
    upperNameFirstChar(name) {
      return upperFirst(name)
    },
    /**
     * 获取slot
     * @param name {string} 插槽名称
     */
    getSlot(name) {
      if (!this.hasSlot(name)) {
        return null
      }
      const slotMap = this.getSlotMap()
      const key = SLOT_PREFIX + this.upperNameFirstChar(name)
      return slotMap[key]
    }
  }
}
